################################################################################################
# This script takes the clean QV data and bootstraps  the exp data to determine welfare under 
# different rules to choose a class and cast the votes. The following rules are tested:
# 
# - Rule A: Choose class and cast vote as chosen in experiment
# - Rule B: 
# - Rule C: 
# - Rule D:  
# - Rule E:
# 
# Rewrites the Class and Vote columns based on these and calculates some welfare measures as in 
# Figures 4 and 5 and Table 2 of the Columbia Elections Paper.
# 
# All data is the saved in SV & QV Simulations (No Recalibration)/QV - Raw Simulation Data/ 
# 
# Author: Luis S.
# Last Modified: 8.25.17
################################################################################################

library(dplyr)
library(ineq)

# setwd("C:/Users/Luis/Dropbox/Research/SV vs QV California Experiment/Experiment Analysis & Results/")

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean_prefrecoded.csv", header = T, stringsAsFactors = F)
QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean_prefrecoded.csv", header = T, stringsAsFactors = F)


##############################################
##### SV - Determine Vector of Utilities #####
#############################################%

SV_data <- SV_data %>%
  mutate(ImmigrationPref = abs(ImmigrationPref), BondsPref = abs(BondsPref), EducationPref = abs(EducationPref), TeachersPref = abs(TeachersPref)) %>%
  select(-matches("Abstain")) %>% select(matches("Ref")) %>%
  mutate(Util_NSV_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_SV1_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_SV2_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_NSV_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_SV1_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_SV2_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_NSV_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_SV1_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_SV2_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_NSV_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0),
         Util_SV1_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0),
         Util_SV2_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0)) %>%
  mutate(Util_NSV = Util_NSV_BilingualEduc + Util_NSV_TeacherTenure + Util_NSV_PublicBonds + Util_NSV_Immigration,
         Util_SV1 = Util_SV1_BilingualEduc + Util_SV1_TeacherTenure + Util_SV1_PublicBonds + Util_SV1_Immigration, 
         Util_SV2 = Util_SV2_BilingualEduc + Util_SV2_TeacherTenure + Util_SV2_PublicBonds + Util_SV2_Immigration)


# Save the vector of realized utilities 
SV_IndivUtility_NSV <- SV_data$Util_NSV
SV_IndivUtility_SV1 <- SV_data$Util_SV1
SV_IndivUtility_SV2 <- SV_data$Util_SV2



##############################################
##### QV - Determine Vector of Utilities #####
#############################################%


QV_data <- QV_data %>% 
  mutate(ImmigrationPref = abs(ImmigrationPref), BondsPref = abs(BondsPref), EducationPref = abs(EducationPref), TeachersPref = abs(TeachersPref)) %>%
  select(-matches("Abstain")) %>% select(matches("Ref")) %>%
  mutate(Util_NQV_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_QV1_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_QV2_BilingualEduc = ifelse(Ref1_BilingualEduc == "InFavor",EducationPref,0),
         Util_NQV_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_QV1_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_QV2_TeacherTenure = ifelse(Ref2_TeacherTenure == "Against",TeachersPref,0),
         Util_NQV_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_QV1_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_QV2_PublicBonds = ifelse(Ref3_PublicBonds == "InFavor",BondsPref,0),
         Util_NQV_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0),
         Util_QV1_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0),
         Util_QV2_Immigration = ifelse(Ref4_Immigration == "Against",ImmigrationPref,0)) %>%
  mutate(Util_NQV = Util_NQV_BilingualEduc + Util_NQV_TeacherTenure + Util_NQV_PublicBonds + Util_NQV_Immigration,
         Util_QV1 = Util_QV1_BilingualEduc + Util_QV1_TeacherTenure + Util_QV1_PublicBonds + Util_QV1_Immigration, 
         Util_QV2 = Util_QV2_BilingualEduc + Util_QV2_TeacherTenure + Util_QV2_PublicBonds + Util_QV2_Immigration)

# Save the vector of realized utilities 
QV_IndivUtility_NQV <- QV_data$Util_NQV
QV_IndivUtility_QV1 <- QV_data$Util_QV1
QV_IndivUtility_QV2 <- QV_data$Util_QV2


#######################################
##### Summarize Gini Coefficients #####
######################################%

NSV <- Gini(SV_IndivUtility_NSV, corr = FALSE)
SV1 <- Gini(SV_IndivUtility_SV1, corr = FALSE)
SV2 <- Gini(SV_IndivUtility_SV2, corr = FALSE)

NQV <- Gini(QV_IndivUtility_NQV, corr = FALSE)
QV1 <- Gini(QV_IndivUtility_QV1, corr = FALSE)
QV2 <- Gini(QV_IndivUtility_QV2, corr = FALSE)



GiniCoeffs <- data.frame(UtilCondition = c("NSV","SV1","SV2","NQV","QV1","QV2"), 
                         GiniCoeff = c(NSV,SV1,SV2,NQV,QV1,QV2), stringsAsFactors = F)

write.csv(GiniCoeffs, "./California Stage 2 - SV & QV Data/Summaries & Tests/Gini Coefficients for SV and QV.csv", row.names = F)


